package org.metasketch.simulator.transformation.demo;

import org.metasketch.demo.model.CoordinationStep;
import org.metasketch.demo.model.TransactionStep;
import org.metasketch.simulator.petri.Link;
import org.metasketch.simulator.petri.Place;
import org.metasketch.simulator.petri.Transition;
import org.metasketch.simulator.resources.ResourceBank;
import org.metasketch.simulator.resources.ResourceBase;

public class StepPetriNet {
	Place before = new Place();
	Transition transition = new Transition();
	Place after = new Place();

	public StepPetriNet() {

	}

	public void setup(TransactionStep step, ResourceBank resources) {

		ResourceBase resourceBase = resources.getResourceBaseByIdentifier()
				.get(step.getActorRole().getId());
		transition.getRequirements().addRequirement(resourceBase, 1l);

		String name = step.getParentTransaction().getName()
				+ " "
				+ (step instanceof CoordinationStep ? ((CoordinationStep) step)
						.getKind() : "Production");

		transition.setName(name + " transition");
		transition.setIdentifier(step.getId() + "-Transition");

		before.setName(name + " start place");
		before.setIdentifier(step.getId() + "-Start");

		after.setName(name + " finish place");
		after.setIdentifier(step.getId() + "-Finish");

		Link startLink = new Link();
		startLink.setSource(before);
		startLink.setTarget(transition);

		before.getOutputs().add(startLink);
		transition.getInputs().add(startLink);

		Link endLink = new Link();
		endLink.setSource(transition);
		endLink.setTarget(after);

		transition.getOutputs().add(endLink);
		after.getInputs().add(endLink);

	}

	public Place getBefore() {
		return before;
	}

	public void setBefore(Place before) {
		this.before = before;
	}

	public Transition getTransition() {
		return transition;
	}

	public void setTransition(Transition transition) {
		this.transition = transition;
	}

	public Place getAfter() {
		return after;
	}

	public void setAfter(Place after) {
		this.after = after;
	}

}
